<!DOCTYPE html>
<html>
<head>

    <title>WebGL 3D Model Viewer Using three.js</title>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="three.js"></script>
    <script src="Detector.js"></script>
    <script src="OrbitControls.js"></script>
    <script src="OBJLoader.js"></script>
    <script src="MTLLoader.js"></script>
    <script src="DDSLoader.js"></script>

    <style>
        body {
            overflow: hidden;
            margin: 0;
            padding: 0;
            background: hsl(0, 0%, 10%);
        }

        p {
            margin: 0;
            padding: 0;
        }

        .left,
        .right {
            position: absolute;
            color: #fff;
            font-family: Geneva, sans-serif;
        }

        .left {
            bottom: 1em;
            left: 1em;
            text-align: left;
        }

        .right {
            top: 0;
            right: 0;
            text-align: right;
        }

        a {
            color: #f58231;
        }
    </style>

</head>
<body style="cursor: move;">
    <img style="position: absolute; top:0px; left: 46%; width: 100px;" id="loadingObj" src="loader.svg">
    <script>
        if (!Detector.webgl) {
            Detector.addGetWebGLMessage();
        }

        var container;

        var camera, controls, scene, renderer;
        var lighting, ambient, keyLight, fillLight, backLight;

        var windowHalfX = window.innerWidth / 2;
        var windowHalfY = window.innerHeight / 2;

        init();
        animate();

        function init() {

            container = document.createElement('div');
            document.body.appendChild(container);

            /* Camera */

            camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
            camera.position.z = 3;

            /* Scene */

            scene = new THREE.Scene();
            lighting = false;

            ambient = new THREE.AmbientLight(0xffffff, 1.0);
            scene.add(ambient);

            keyLight = new THREE.DirectionalLight(new THREE.Color('hsl(30, 100%, 75%)'), 1.0);
            keyLight.position.set(-100, 0, 100);

            fillLight = new THREE.DirectionalLight(new THREE.Color('hsl(240, 100%, 75%)'), 0.75);
            fillLight.position.set(100, 0, 100);

            backLight = new THREE.DirectionalLight(0xffffff, 1.0);
            backLight.position.set(100, 0, -100).normalize();

            /* Model */
            var urlPath = getQueryVariable("folder");
            var mtlFile = getQueryVariable("mtl");
            var objFile = getQueryVariable("obj");
            
            console.log("Load 3D File: ",urlPath,mtlFile,objFile );
            THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );

            if(mtlFile!="") {
                var mtlLoader = new THREE.MTLLoader();
                mtlLoader.setBaseUrl(urlPath);
                mtlLoader.setPath(urlPath);
                mtlLoader.load(mtlFile, function (materials) {

                    materials.preload();

                    for(var i in materials.materials) {
                        if(materials.materials[i].map) {
                            materials.materials[i].map.magFilter = THREE.NearestFilter;
                            materials.materials[i].map.minFilter = THREE.LinearFilter;
                        }
                    }

                    var objLoader = new THREE.OBJLoader();
                    objLoader.setMaterials(materials);
                    objLoader.setPath(urlPath);
                    objLoader.load(objFile, function (object) {
                        document.getElementById("loadingObj").remove();
                        scene.add(object);
                    });

                });
            } else {
                var objLoader = new THREE.OBJLoader();
                objLoader.setPath(urlPath);
                objLoader.load(objFile, function (object) {
                    document.getElementById("loadingObj").remove();
                    scene.add(object);
                });
            }
            
            ambient.intensity = 0.5;
            scene.add(keyLight);
            scene.add(fillLight);
            scene.add(backLight);

            /* Renderer */

            renderer = new THREE.WebGLRenderer({"antialias" : true});
            renderer.setPixelRatio(window.devicePixelRatio);
            renderer.setSize(window.innerWidth, window.innerHeight);
            renderer.setClearColor(new THREE.Color("hsl(0, 0%, 10%)"));

            container.appendChild(renderer.domElement);

            /* Controls */

            controls = new THREE.OrbitControls(camera, renderer.domElement);
            controls.enableDamping = true;
            controls.dampingFactor = 0.25;
            controls.enableZoom = true;
            controls.enableKeys = true;

            /* Events */

            window.addEventListener('resize', onWindowResize, false);

        }

        function onWindowResize() {

            windowHalfX = window.innerWidth / 2;
            windowHalfY = window.innerHeight / 2;

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize(window.innerWidth, window.innerHeight);

        }

        function animate() {
            requestAnimationFrame(animate);

            controls.update();
            render();

        }

        function render() {
            renderer.render(scene, camera);

        }

        function getQueryVariable(variable) {
          var query = window.location.search.substring(1);
          var vars = query.split("&");
          for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
              return pair[1];
            }
          } 
          console.log('Query Variable ' + variable + ' not found');
        }

    </script>

</body>
</html>
